<template>
  <basic-container>
    <el-container>
      <el-aside width="200px">
        <avue-tree :option="treeOption" :data="treeData" @node-click="nodeClick"></avue-tree>
      </el-aside>
      <el-main style="margin-left: 10px">
        <avue-crud
          :option="option"
          :table-loading="loading"
          :data="data"
          :page.sync="page"
          :search.sync="query"
          v-model="form"
          @row-del="rowDel"
          @search-change="searchChange"
          @search-reset="searchReset"
          @selection-change="selectionChange"
          @current-change="currentChange"
          @size-change="sizeChange"
          @refresh-change="onLoad(page, query)"
        >
          <template slot="menuLeft">
            <el-button type="danger" plain @click="handleDelete">删除 </el-button>
          </template>
        </avue-crud>
      </el-main>
    </el-container>

    <el-dialog :visible.sync="viewVisible" title="模型预览" destroy-on-close append-to-body>
      <wf-design ref="bpmn" style="height: 500px" :options="viewOption"></wf-design>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, remove, setMainVersion } from '@/api/plugin/workflow/model-history'
import { tree } from '@/api/plugin/workflow/category'

export default {
  props: ['param', 'permission'],
  data() {
    return {
      formVisible: false,
      options: {},
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        size: 'mini',
        height: 'auto',
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        dialogType: 'drawer',
        align: 'center',
        searchMenuSpan: 6,
        menuWidth: 150,
        column: [
          {
            label: '模型key',
            prop: 'modelKey',
            overHidden: true,
            search: true
          },
          {
            label: '模型名称',
            prop: 'name',
            overHidden: true,
            search: true
          },
          {
            label: '分类',
            prop: 'categoryId',
            type: 'tree',
            props: {
              label: 'name',
              value: 'id'
            },
            dicData: [],
            rules: [
              {
                required: true,
                message: '请选择分类',
                trigger: 'change'
              }
            ]
          },
          {
            label: '描述',
            prop: 'description',
            overHidden: true
          },
          {
            label: '版本',
            prop: 'version',
            width: 80
          }
        ],
        operatingButtons: this.getOperatingButtons
      },
      data: [],
      viewVisible: false,
      viewOption: {},
      treeData: [],
      treeOption: {
        size: 'mini',
        addBtn: false,
        props: {
          label: 'name',
          value: 'id'
        }
      }
    }
  },
  computed: {
    ids() {
      let ids = []
      this.selectionList.forEach(ele => {
        ids.push(ele.id)
      })
      return ids.join(',')
    }
  },
  mounted() {
    this.handler()
    this.getCategoryList()
  },
  methods: {
    getOperatingButtons() {
      const preview = { text: '预览', callback: this.handlePreview }
      const main = { text: '设为主版本', callback: this.handleMain }

      let buttons = []
      if (this.permission.wf_design_form_history_view) buttons.push(preview)
      if (this.permission.wf_design_form_history_main) buttons.push(main)
      return buttons
    },
    handler() {
      this.modelId = this.param
      this.onLoad(this.page, this.query)
    },
    getCategoryList() {
      tree().then(res => {
        const data = res.data.data
        this.findObject(this.option.column, 'categoryId').dicData = this.deepClone(data)

        this.treeData = data
        this.treeData.unshift({ id: '', name: '全部' })
      })
    },
    nodeClick({ id }) {
      this.categoryId = id
      this.searchChange(this.query)
    },
    handleMain(row) {
      this.$confirm('当前主版本会自动保存到历史，确定要将此版本设为主版本吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        setMainVersion({ id: row.id }).then(() => {
          this.$message.success('操作成功')
          // this.$store.commit('DEL_TAG', this.tag)
          // this.$router.push('/plugin/workflow/design/model')
        })
      })
    },
    handlePreview(row) {
      this.viewOption = {
        mode: 'view',
        xml: row.xml
      }
      this.viewVisible = true
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.$refs.crud.toggleSelection()
        })
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.onLoad(this.page, params)
      if (done) done()
    },
    selectionChange(list) {
      this.selectionList = list
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
      this.onLoad(this.page, this.query)
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
      this.onLoad(this.page, this.query)
    },
    onLoad(page, params = {}) {
      this.loading = true
      params.modelId = this.modelId

      if (this.categoryId) params['categoryId'] = this.categoryId
      else delete params['categoryId']

      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data
        this.page.total = data.total
        this.data = data.records
        this.loading = false
      })
    }
  }
}
</script>

<style></style>
